package sg.edu.nus.iss.phoenix.schedule.controller;

import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sg.edu.nus.iss.phoenix.core.controller.FCUtilities;
import sg.edu.nus.iss.phoenix.schedule.delegate.ScheduleDelegate;
import sg.edu.nus.iss.phoenix.schedule.entity.Schedule;
import sg.edu.nus.iss.phoenix.schedule.service.DateUtil;

/**
 * 
 * @author Gayathri, Karthic Kumar
 *
 */
@WebServlet("/ReviewSelectScheduledProgramController/*")
public class ReviewSelectScheduledProgramController extends HttpServlet {

	private static final long serialVersionUID = -1805723089942826756L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			processRequest(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher temp = request.getRequestDispatcher("/pages/exception.jsp");
			temp.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			processRequest(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher temp = request.getRequestDispatcher("/pages/exception.jsp");
			temp.forward(request, response);
		}
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String selection = FCUtilities.stripPath(request.getPathInfo()).toLowerCase();
		if(!(request.getSession().getAttribute("user")==null)){
			String date = request.getParameter("selecteddate");
			
			request.getSession().setAttribute("date", date);
			ScheduleDelegate sd1 = new ScheduleDelegate();
			
			if(date == null){
				date = DateUtil.convertToJavaDateFormat(DateUtil.getCurrentDate().toString());
			}if(date != null && date.equals("")){
				date = DateUtil.convertToJavaDateFormat(DateUtil.getCurrentDate().toString());
			}
			
			String toDateString = date;
			DateFormat formater = new SimpleDateFormat("dd-MM-yyyy");  
			java.util.Date parsedUtilDate = formater.parse(toDateString);  
			java.sql.Date sqltDate= new java.sql.Date(parsedUtilDate.getTime());
			
	
			String toDateString1 = "2012-09-24";
			DateFormat formater1 = new SimpleDateFormat("yyyy-MM-dd");  
			java.util.Date parsedUtilDate1 = formater1.parse(toDateString1);  
			java.sql.Date sqltDate1= new java.sql.Date(parsedUtilDate1.getTime());
			
			Map<Date, Schedule> weeklySchedule = sd1.getWeeklySchedule(sqltDate);//DateUtil.getCurrentDate());			
			
			request.getSession().setAttribute("weeklySchedule", weeklySchedule);
			request.getSession().setAttribute("daysOfWeek", DateUtil.getDays());
			request.getSession().setAttribute("allTimeSlots", DateUtil.getAllTimings());
			request.getSession().setAttribute("date", date);
			
			request.setAttribute("fromDate", DateUtil.getCurrentDate());
			request.setAttribute("toDate", DateUtil.getCurrentDate());
			RequestDispatcher rd2 = request.getRequestDispatcher("/pages/crudschedule.jsp");
			rd2.forward(request, response);
		}
		else{
			RequestDispatcher rd = request.getRequestDispatcher("controller/pages/login.jsp");
			rd.forward(request, response);
		}
	}
}
